package cn.edu.ujn.booksshop.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.booksshop.dao.CartDetail;
import cn.edu.ujn.booksshop.dao.ShoppingCart;
import cn.edu.ujn.booksshop.service.ICartDetailService;
import cn.edu.ujn.util.Page;

@Controller
public class CartDetailController {

	
	
	@Autowired
	private ICartDetailService cartDetail;
	

	
	@GetMapping("/cartDetail/list.action")
	public String list(Model model,@RequestParam (defaultValue = "1")  Integer page ,@RequestParam(defaultValue ="10")  Integer rows) {
		Page<CartDetail> pages = this.cartDetail.selectAll(page,rows);
		model.addAttribute("page",pages);
		return "cartDetail";
	}
	
	@GetMapping("/cartDetail/getCartDetailById.action")
	@ResponseBody
	public CartDetail getCartDetailById(@RequestParam Integer id) {
		CartDetail cart = cartDetail.selectByPrimaryKey(id);
		return cart;
	}
	
	@PostMapping("/cartDetail/update.action")
	public ResponseEntity<String> list2(@ModelAttribute CartDetail sc) {
		int key = cartDetail.updateByPrimaryKey(sc);
		if(key==1) {
			return ResponseEntity.ok("OK");
		}
		return ResponseEntity.ok("NO");
	}	

	@PostMapping("/cartDetail/delete.action")
	public ResponseEntity<String> delete(@RequestParam Integer id) {
		int key2 = cartDetail.deleteByPrimaryKey(id);	
		if(key2 == 1) {
			return ResponseEntity.ok("OK");
		}
		return ResponseEntity.ok("NO");
	}
	
	@PostMapping("/createCartDetail/create.action")
	public ResponseEntity<String> create(@ModelAttribute CartDetail sc){
		int insert = cartDetail.insert(sc);
		if(insert == 1) {
			return ResponseEntity.ok("OK");
		}
		return ResponseEntity.ok("NO");
	}
}
